import React from 'react';
import { connect } from 'dva';
import { Divider } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';

@connect(({ quotation, publicdata, loading }) => ({
  uploading: loading.effects['quotation/uploadFileWithProgress'],
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  quotation,
}))
class Batch extends React.PureComponent {
  componentDidMount() {
    const { dispatch } = this.props;
    // 下载模板
    dispatch({
      type: 'quotation/getDownloadInfo',
      payload: {
        tempSerType: 57,
      },
    });
    this.clearModel();
  }

  componentWillUnmount() {
    this.clearModel();
  }

  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'quotation/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'quotation/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch } = this.props;
    dispatch({
      type: 'quotation/uploadFileWithProgress',
      payload: formData,
    });
  };

  render() {
    const isExactDiv = () => (
      <div>
        <Divider dashed style={{ marginTop: 100 }} />
        <p style={{ margin: '0 auto', width: '90%', fontSize: 14, textAlign: 'left' }}>
          字段维护规则：
          <br />
          1、产品名称：打包产品放在一行，用+号隔开；不在一个包内的产品需分行维护；
          <br />
          2、地区和价格：相同产品不同地区及价格需要分行维护；
          <br />
          3、社保缴纳险种：如果五险全部，则维护‘’全部‘’；如其中某几个险种，维护具体险种并用+号隔开；单个险种直接维护即可。
        </p>
      </div>
    );
    const {
      fileId,
      fileName,
      uploading,
      quotation: { currentStep, showProgress, progressPercent, resultInfo },
    } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: { fileId, fileName },
              url: '/csApi/batchTask/downloadFile',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            showProgress={showProgress}
            progressPercent={progressPercent || 0}
            progressTitle="批量新增任务"
            progressVisible={showProgress}
            handleModalVisible={this.handleModalVisible}
            isExact={isExactDiv}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
